#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=blowfish.ico
#AutoIt3Wrapper_outfile=..\..\..\Programme\yafcrypt\yafcrypt_gui.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;yafcrypt GUI for MS Windows(tm)
;copyright 2010 Manuel Reinelt
;mrdevlab@googlemail.com
;receives a command from a file-rightclick
;registry-entries:
;HKEY_CLASSES_ROOT\*\Shell\decrypt\command
;HKEY_CLASSES_ROOT\*\Shell\encrypt\command

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GuiEdit.au3>
#Include <GuiButton.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <Array.au3>
#Include <Misc.au3>

;set some autoit options:


AutoItSetOption("MustDeclareVars",1)
AutoItSetOption("TrayIconHide",1)

Global $FormFilecrypt
Global $InputPassphrase
Global $LabelPassprhase
Global $Labelvisible
Global $Checkboxvisible
Global $LabelWipeSource
Global $CheckboxWipeSource
Global $Lableatlevel
Global $ComboWipelevel
Global $CheckboxVerbose
Global $ComboCipher
Global $LabelVerbose
Global $ButtonCrypt
Global $EditLog
Global $ProgressChunks
Global $ProgressBytes
Global $InputFilename
Global $LabelFilename
Global $ButtonFilebrowse

Local $pid
Local $stdouterr
Local $stdout
Local $stderr
Local $installpath
Local $cipher

Const $guiexecutable = "yafcrypt_gui.exe"
Const $subexecutable = "file_crypt.exe"

Global  $percentarray
Global  $progress

Global $FileName

Global $commandstring = ""
Global $commandarray[1]

Opt("GUIOnEventMode", 1)

#Region ### START Koda GUI section ### Form=FormFilecrypt.kxf
$FormFilecrypt = GUICreate("Yafcrypt 0.0.0.31", 403, 407, 192, 114)
GUISetIcon("blowfish.ico", -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "FormFilecryptClose")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "FormFilecryptMinimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "FormFilecryptMaximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "FormFilecryptRestore")
$InputPassphrase = GUICtrlCreateInput("", 80, 16, 241, 21, $GUI_SS_DEFAULT_INPUT)
GUICtrlSetOnEvent(-1, "InputPassphraseChange")
GUICtrlSetTip(-1, "Enter a passphrase. Maximum 56 characters")
$LabelPassprhase = GUICtrlCreateLabel("Passprhase:", 10, 16, 60, 18, $SS_CENTERIMAGE)
GUICtrlSetOnEvent(-1, "LabelPassprhaseClick")
$Labelvisible = GUICtrlCreateLabel("visible", 328, 16, 33, 17, BitOR($SS_RIGHT,$SS_CENTERIMAGE))
GUICtrlSetOnEvent(-1, "LabelvisibleClick")
$Checkboxvisible = GUICtrlCreateCheckbox("Checkboxvisible", 376, 16, 17, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetOnEvent(-1, "CheckboxvisibleClick")
$LabelWipeSource = GUICtrlCreateLabel("Wipe Source", 8, 48, 66, 17, 0)
GUICtrlSetOnEvent(-1, "LabelWipeSourceClick")
$CheckboxWipeSource = GUICtrlCreateCheckbox("Checkboxvisible", 80, 48, 17, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetOnEvent(-1, "CheckboxWipeSourceClick")
$lableatlevel = GUICtrlCreateLabel("@level ", 96, 48, 40, 17, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetOnEvent(-1, "lableatlevelClick")
$ComboWipelevel = GUICtrlCreateCombo("", 144, 48, 33, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "1|2|3", "1")
GUICtrlSetOnEvent(-1, "ComboWipelevelChange")
$CheckboxVerbose = GUICtrlCreateCheckbox("Checkboxvisible", 184, 48, 17, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetOnEvent(-1, "CheckboxVerboseClick")
$ComboCipher = GUICtrlCreateCombo("", 253, 48, 65, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "blowfish|aes", "blowfish")
GUICtrlSetOnEvent(-1, "ComboCipherChange")
$LabelVerbose = GUICtrlCreateLabel("Verbose", 201, 43, 44, 26, $SS_CENTERIMAGE)
GUICtrlSetOnEvent(-1, "LabelVerboseClick")
$ButtonCrypt = GUICtrlCreateButton("Encrypt", 328, 48, 65, 25, BitOR($BS_CENTER,$BS_NOTIFY))
GUICtrlSetOnEvent(-1, "ButtonCryptClick")
$EditLog = GUICtrlCreateEdit("", 8, 116, 385, 213, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$WS_VSCROLL))
GUICtrlSetData(-1, StringFormat("\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"))
GUICtrlSetOnEvent(-1, "EditLogChange")
$ProgressChunks = GUICtrlCreateProgress(7, 347, 385, 16, 0)
$ProgressBytes = GUICtrlCreateProgress(6, 375, 385, 17, $PBS_SMOOTH)
$LabelFilename = GUICtrlCreateLabel("Filename:", 17, 81, 49, 17)
GUICtrlSetOnEvent(-1, "LabelFilenameClick")
$InputFilename = GUICtrlCreateInput("", 79, 77, 241, 21, $GUI_SS_DEFAULT_INPUT)
GUICtrlSetOnEvent(-1, "InputFilenameChange")
$ButtonFilebrowse = GUICtrlCreateButton("...", 341, 79, 29, 20)
GUICtrlSetOnEvent(-1, "ButtonFilebrowseClick")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###



Init()

While 1
	Sleep(100)

	;start when InputPasssphrase is in focus and <return> is pressed:
	If _IsPressed("0D")  And  ControlGetFocus("Filecrypt") == "Edit1" Then
		ButtonCryptClick()
	EndIf

WEnd


Func Init()
	If UBound($CmdLine) == 1 Then
		#Region --- CodeWizard generated code Start ---
		;MsgBox features: Title=Yes, Text=Yes, Buttons=OK, Icon=Warning
		MsgBox(48,"Error","This application must be invoked with arguments" & @CRLF & "(i. e. from context-menu)")
		#EndRegion --- CodeWizard generated code End ---
		Exit(-1)
	EndIf
	InitCommandArray()
	SetEncDec()
	SetFileName()
	ComboCipherChange()
EndFunc

Func ButtonCryptClick()

	If _GUICtrlEdit_GetText($InputPassphrase) == "" Then
		MsgBox(0,"Error","Enter a passhrase")
	ElseIf StringLen(_GUICtrlEdit_GetText($InputPassphrase)) > 56 Then
		MsgBox(0,"Error","Keylength > 56 characters")
	Else

		SetEncDecArgs()
		BuildCommandString()


		$installpath = StringSplit(RegRead("HKEY_CLASSES_ROOT\*\Shell\encrypt\command",""),$guiexecutable,1)

		$commandstring = $installpath[1] & $subexecutable & $commandstring

		_GUICtrlEdit_SetText($EditLog,"executing " & $commandstring)

		$pid = Run(@ComSpec & " /c " & $commandstring, "", @SW_HIDE,$STDERR_CHILD + $STDOUT_CHILD)

		$stdouterr = ""

		While 1

			$stdout = ""

			$stdout = StdoutRead ($pid,0,0)
			$stderr = StderrRead ($pid,0,0)

			If @error  Then
				ExitLoop
			ElseIf StringInStr($stdout,"ERROR:") Then
				_GUICtrlEdit_AppendText($EditLog,$stdout)
				_GUICtrlEdit_AppendText($EditLog,$stderr)
				ExitLoop
			ElseIf StringLen($stdout) > 0 Then

				If StringInStr($stdout,"chunk encoding-progress:") Then
					$percentarray = StringSplit($stdout,"%",2)
					$progress = StringStripWS($percentarray[1],8)
					GUICtrlSetData($ProgressChunks,int($progress))
				ElseIf StringInStr($stdout,"byte encoding-progress:") Then
					$percentarray = StringSplit($stdout,"%",2)
					$progress = StringStripWS($percentarray[1],8)
					GUICtrlSetData($ProgressBytes,int($progress))
				EndIf

				If Not  StringInStr($stdout,"byte encoding-progress:") Then
					_GUICtrlEdit_AppendText($EditLog,$stdout)
				EndIf

			EndIf

		Wend

		GUICtrlSetData($ProgressChunks,0)
		GUICtrlSetData($ProgressBytes,0)

		If StringLen($stderr) > 0 Or  StringInStr($stdout,"ERROR:") Then
			MsgBox(48,"ERROR","An error occured! Check log-window!")
		Else
			MsgBox(64,"OK","Successfully completed!")
		EndIf

	EndIf

	;reinit:
	Init()

EndFunc

Func InitCommandArray()

		Global $commandarray[1]

		Local $excludeparams = StringSplit("-e,-d,-v",2)
		Local $param = ""

		For $i = 1 To UBound($CmdLine) - 2

			$param = $CmdLine[$i]

			If  _ArraySearch($excludeparams,$CmdLine[$i] ,0,0,0,1,0,0) == -1 Then
					$param &= " " & $CmdLine[$i + 1]
					$i += 1
			EndIf
			_ArrayAdd($commandarray,$param)

		Next

		_ArrayDelete($commandarray,0)

EndFunc

Func SetEncDec()

	For $i in $commandarray

		If $i == "-e" Then
			_GUICtrlButton_SetText($ButtonCrypt,"Encrypt")
		ElseIf $i == "-d" Then
			_GUICtrlButton_SetText($ButtonCrypt,"Decrypt")
		EndIf

	Next

EndFunc

Func SetEncDecArgs()

	If _ArraySearch($commandarray,"-e",0,0,0,1,0,0) <> -1 Then
		$commandarray[_ArraySearch($commandarray,"-e",0,0,0,1,0,0)] = "-e " & '"' & _GUICtrlEdit_GetText($InputPassphrase) & '"'
	ElseIf _ArraySearch($commandarray,"-d",0,0,0,1,0,0) <> -1 Then
		$commandarray[_ArraySearch($commandarray,"-d",0,0,0,1,0,0)] = "-d " & '"' & _GUICtrlEdit_GetText($InputPassphrase) & '"'
	Else
		Exit
	EndIf

EndFunc

Func SetFileName()

	local $x = 0
	local $y = 0

	$FileName = $commandarray[_ArraySearch($commandarray,"-i",0,0,0,1,0,0)]
	$x = StringInStr($FileName,"|") + 1
	$y = StringInStr($FileName,"|",0,-1,1,StringLen($FileName))
	$FileName = StringMid($FileName,$x,Stringlen($FileName) - $x)
	_GUICtrlEdit_SetText($InputFilename,$FileName)

EndFunc

Func UpdateFileName()
	$commandarray[_ArraySearch($commandarray,"-i",0,0,0,1,0,0)] = "-i " & $FileName
	_GUICtrlEdit_SetText($InputFilename,$FileName)
EndFunc

Func BuildCommandString()
	$commandstring = ""

	For $i in $commandarray
		If StringLen($i) > 0 Then
			$commandstring &= " " & $i
		EndIf
	Next
	;remove the delimiters from the commandline we receive:
	$commandstring = StringReplace($commandstring,"|",'"')


EndFunc

Func CheckboxVerboseClick()

	If BitAND(GUICtrlRead($CheckboxVerbose),$GUI_CHECKED) Then
		_ArrayAdd($commandarray,"-v")
	Else
		_ArrayDelete($commandarray,_ArraySearch($commandarray,"-v",0,0,0,1,0,0))
	EndIf

EndFunc

Func CheckboxWipeSourceClick()

	If BitAND(GUICtrlRead($CheckboxWipeSource),$GUI_CHECKED) Then
		_ArrayAdd($commandarray,"-w " & GUICtrlRead($ComboWipelevel,1))
	Else

		_ArrayDelete($commandarray,_ArraySearch($commandarray,"-w" & GUICtrlRead($ComboWipelevel,1),0,0,0,1,0,0))

	EndIf

EndFunc

Func ComboWipelevelChange()

	If _ArraySearch($commandarray,"-w " & $ComboWipelevel) > 0 Then
		_ArrayDelete($commandarray,_ArraySearch($commandarray,"-w " & $ComboWipelevel,0,0,0,1,0,0))
	EndIf

	_ArrayAdd($commandarray,"-w " & GUICtrlRead($ComboWipelevel,1))

EndFunc

Func CheckboxvisibleClick()

	If BitAND(GUICtrlRead($CheckboxVisible),$GUI_CHECKED) Then
		_GUICtrlEdit_SetPasswordChar($InputPassphrase,0)
	Else
		_GUICtrlEdit_SetPasswordChar($InputPassphrase,"*")
	EndIf

EndFunc

Func ComboCipherChange()

	$cipher = GUICtrlRead($ComboCipher,1)

	If $cipher == "aes" And _ArraySearch($commandarray,"-c blowfish") > 0 Then
		$commandarray[_ArraySearch($commandarray,"-c blowfish")] = "-c aes"
	ElseIf $cipher == "blowfish" And _ArraySearch($commandarray,"-c aes") > 0 Then
		$commandarray[_ArraySearch($commandarray,"-c aes")] = "-c blowfish"
	EndIf

EndFunc

Func ButtonFilebrowseClick()

	$FileName = FileOpenDialog("Choose File", "C:\","All (*.*)",1 + 4 )
	UpdateFileName()

EndFunc

Func FormFilecryptClose()
	Exit
EndFunc

;yet unused functions:

Func FormFilecryptMinimize()
EndFunc

Func FormFilecryptMaximize()
EndFunc

Func FormFilecryptRestore()
EndFunc

Func InputPassphraseChange()
EndFunc

Func  LabelPassprhaseClick()
EndFunc

Func LabelvisibleClick()
EndFunc

Func LabelWipeSourceClick()
EndFunc

Func lableatlevelClick()
EndFunc

Func LabelVerboseClick()
EndFunc

Func EditLogChange()
EndFunc

Func LabelFilenameClick()
EndFunc

Func InputFilenameChange()
EndFunc

